//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    modalActive:false,
    domain:app.globalData.baseUrl,
    banner:[],
    systeminfos:'',
    date: '',
    startdate: '',
    enddate: '',
    tdate: '',
    alldays:1,
    tstartdate: '',
    tenddate: '',
    homestay: [],
    homeModal:{
      des:'',
      bed:'',
      storey:'',
      area:'',
      breakfast:'',
      peoples:'',
      wifi:'',
      more_bed:'',
      non_smoking:'',
      price:'',
      imgs:[]
    }
  },
  onLoad: function () {
    var that=this
    wx.request({
      url: app.globalData.baseUrl + '/api/index/banner',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          banner:res.data.data
        })
      }
    });
    
    wx.request({
      url: app.globalData.baseUrl + '/api/index/systeminfos',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          systeminfos:res.data.data
        })
      }
    });
    wx.request({
      url: app.globalData.baseUrl + '/api/index/rent_date',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          date:res.data.date,
          startdate:res.data.startdate,
          enddate:res.data.enddate,
          tdate:res.data.tdate,
          tstartdate:res.data.tstartdate,
          tenddate:res.data.tenddate
        })
      }
    });
    wx.request({
      url: app.globalData.baseUrl + '/api/index/homestay',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          homestay:res.data.data
        })
      }
    });
  },
  makePhone: function () {
    var that=this
    wx.makePhoneCall({
      phoneNumber: that.data.systeminfos.telphone
    });
  },
  toMap:function(){
    var that=this
      wx.openLocation({
        latitude:parseFloat(that.data.systeminfos.latitude),
        longitude:parseFloat(that.data.systeminfos.longitude),
        name:'[位置]',
        address:that.data.systeminfos.address,
        scale: 18
      })
      // wx.openLocation({
      //   latitude:29.823966,
      //   longitude:112.116766,
      //   name:'[位置]',
      //   address:'湖北省荆州市公安县青龙村13组',
      //   scale: 18
      // })
  },
  toComments:function(){
    wx.navigateTo({
      url: '../comments/comments'
    })
  },
  bindDateChangef: function(e) {
    let d1 = new Date(e.detail.value.replace(/-/g, "/"));
    let d2 = new Date(this.data.tdate.replace(/-/g, "/"));
    let times = d2.getTime() - d1.getTime();
    let alldays = parseInt(times / (1000 * 60 * 60 * 24));
    this.setData({
      date: e.detail.value,
      alldays: alldays
    })
  },
  bindDateChanget: function(e) {
    let d1 = new Date(this.data.date.replace(/-/g, "/"));
    let d2 = new Date(e.detail.value.replace(/-/g, "/"));
    let times = d2.getTime() - d1.getTime();
    let alldays = parseInt(times / (1000 * 60 * 60 * 24));
    this.setData({
      tdate: e.detail.value,
      alldays: alldays
    })
  },
  orderIn: function(e) {
    var that=this
    if(that.data.date==undefined||that.data.tdate==undefined){
      wx.showToast({
        icon:'none',
        title: '未选择入住日期',
        duration: 2000
      })
      return
    }
    wx.request({
      url: app.globalData.baseUrl + '/api/index/is_rented',
      data: {
        id:e.currentTarget.dataset.id,
        date:that.data.date,
        tdate:that.data.tdate
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if(res.data.code==200){
          wx.navigateTo({
            url: '../orderin/orderin?date='+that.data.date+'&tdate='+that.data.tdate+'&homestay='+JSON.stringify(that.data.homestay[e.currentTarget.dataset.index])
          })
        }else if(res.data.code==201){
          wx.showModal({
            title: res.data.msg,
            content: res.data.data.toString(),
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else{
          wx.showToast({
            icon:'none',
            title: res.data.msg,
            duration: 2000
          })
        }
      }
    });
  },
  homeModal:function(e){
    var that=this
    this.setData({
      modalActive:true,
      homeModal:that.data.homestay[e.currentTarget.dataset.index]
    })
  },
  modalDis:function(){
    var that=this
    this.setData({
      modalActive:false
    })
  },
  onShareAppMessage:function(){
    return {
      title: '龙门客栈',
      path: '/pages/index/index',
      imageUrl:'/img/share.png',
      success: function(res) {}
    }
  }
})
